package com.noble.meether.ui.image;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.noble.glamour.module.logic.baseui.custom.TouchImageView;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.meether.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 像片放大页面
 */
public class PhotoDisplayAct extends BaseAppCompatActivity implements OnClickListener, OnPageChangeListener {
    public final static int DISPLAY_TYPE_BIG_IMG = 4; //看大图

    private ViewPager vp_photo_display;
    private TextView btn_photo_display_title;

    private List<String> imageList;
    private int currentPosition;
    private int displayType;

    private PhotoDiaplayAdapter displayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isCanBack(false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p1_photo_display_act);
        initData();
        initView();
        initEvent();
    }

    @Override
    protected boolean isImmersionBar() {
        return false;
    }

    @SuppressWarnings("unchecked")
    private void initData() {
        currentPosition = getIntent().getIntExtra("position", 0);
        displayType = getIntent().getIntExtra("type", 1);
        if (displayType == DISPLAY_TYPE_BIG_IMG) {
            imageList = (List<String>) getIntent().getSerializableExtra("list");
            if (imageList == null) {
                imageList = new ArrayList<>();
            }
            displayAdapter = new PhotoDiaplayAdapter(getSupportFragmentManager(), imageList);
        }
        displayAdapter.setOnClickEvent(onClickEvent);
    }

    private TouchImageView.OnClickEvent onClickEvent = view -> {
        //onBackPressed();
    };

    private void initView() {
        this.vp_photo_display = findViewById(R.id.vp_photo_display);
        this.btn_photo_display_title = findViewById(R.id.btn_photo_display_title);

        int imgSize;
        imgSize = imageList.size();

        if (imgSize > 1) {
            btn_photo_display_title.setVisibility(View.VISIBLE);
            btn_photo_display_title.setText("照片  " + (currentPosition + 1) + "/" + imgSize);
        }

        this.vp_photo_display.setAdapter(displayAdapter);
    }

    private void initEvent() {
        this.vp_photo_display.setOnPageChangeListener(this);
        findViewById(R.id.btn_title_back).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_title_back:
                finish();
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int position) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int position) {
        currentPosition = position;
        btn_photo_display_title.setText("照片  " + (position + 1) + "/" + imageList.size());
    }

    @Override
    protected void onResume() {
        super.onResume();
        vp_photo_display.setCurrentItem(currentPosition);
    }
}
